package com.yc.demo.servlet;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet("/cookie.s")
public class CookieServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		/**
		 * cookie 于服务器有交互行为
			从浏览器发出的请求会自动带回cookie数据
		 */
		Cookie[] cookies = request.getCookies();
		response.setContentType("text/html;charset=utf-8");
		
		// 打印请求中的cookie数据
		if (cookies != null) {
			for(Cookie c : cookies) {
				response.getWriter().println("name:" + c.getName());
				response.getWriter().println("<br>");
				response.getWriter().println("value:" + c.getValue());
				response.getWriter().println("<br>");
				response.getWriter().println("maxAge:" + c.getMaxAge());
				response.getWriter().println("+++++++++++++++++++<br>");
			}
		}
		
		// 创建cookie
		Cookie c = new Cookie("name" , "zhangsan");
		response.addCookie(c);

	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

	}

}
